package com.example.boxueguapplication;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.boxueguapplication.model.ShiPin;

import java.util.Arrays;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class ShipinFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private int lastPosition;
    private LinearLayout banner_d;

    public ShipinFragment() {
    }

    public ShipinFragment newInstance(String param1, String param2) {
        ShipinFragment fragment = new ShipinFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        ShiPinData = ShiPin.listAll(ShiPin.class);
        if (ShiPinData.isEmpty()) {
            ShiPin.saveInTx(
                    new ShiPin(1L, "第1章 Android 基础入门", "Android 开发环境搭建", R.drawable.chapter_1_icon),
                    new ShiPin(2L, "第2章 Android UI开发", "Android 五大布局", R.drawable.chapter_2_icon),
                    new ShiPin(3L, "第3章 Activity", "Android 的使用", R.drawable.chapter_3_icon)
            );
            ShiPin.saveInTx();
            ShiPinData=ShiPin.listAll(ShiPin.class);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_shipin, container, false);
    }

    private RelativeLayout appBar;
    private ImageView back;
    private TextView titleBar;
    private ViewPager2 banner;
    private Timer timer;
    private List<ShiPin> ShiPinData;
    private List<Integer> BannerData;
    private RecyclerView mShiPinList;


    private void findView(View view) {
        appBar = (RelativeLayout) view.findViewById(R.id.app_bar);
        back = (ImageView) view.findViewById(R.id.back);
        titleBar = (TextView) view.findViewById(R.id.title_bar);
        banner = view.findViewById(R.id.banner);
        banner_d = view.findViewById(R.id.banner_D);
        mShiPinList = view.findViewById(R.id.shipin_list);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        findView(view);
        back.setVisibility(View.GONE);
        titleBar.setText("课程");
        BannerData = Arrays.asList(R.drawable.banner_1, R.drawable.banner_2, R.drawable.banner_3);
        banner.setAdapter(new bannerAdapter(BannerData));
        lastPosition = banner.getCurrentItem();
        banner.registerOnPageChangeCallback(new OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                int current = position % BannerData.size();
                int last = lastPosition % BannerData.size();
                ((CardView) banner_d.getChildAt(current)).setCardBackgroundColor(0xff0099cc);
                ((CardView) banner_d.getChildAt(last)).setCardBackgroundColor(0xff666666);
                lastPosition = position;
            }
        });
        mShiPinList.setLayoutManager(new GridLayoutManager(getContext(), 2));
        mShiPinList.setAdapter(new ShiPinAdapter(R.layout.shipin_item, ShiPinData));
    }

    @Override
    public void onResume() {
        super.onResume();
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                banner.setCurrentItem(banner.getCurrentItem() + 1 % BannerData.size());
            }
        }, 0, 3000);
    }

    @Override
    public void onPause() {
        super.onPause();
        timer.cancel();
    }

    static class bannerAdapter extends RecyclerView.Adapter<bannerAdapter.ViewHolder> {

        private List<Integer> data;

        public bannerAdapter(List<Integer> data) {
            this.data = data;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.banner_item, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            int i = position % data.size();
            holder.mbanner.setBackgroundResource(data.get(i));
        }


        @Override
        public int getItemCount() {
            return Integer.MAX_VALUE;
        }

        static class ViewHolder extends RecyclerView.ViewHolder {

            RelativeLayout mbanner;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                mbanner = itemView.findViewById(R.id.banner_item);
            }
        }
    }

    static class ShiPinAdapter extends BaseQuickAdapter<ShiPin, BaseViewHolder> {

        public ShiPinAdapter(int layoutResId, List<ShiPin> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder holder, ShiPin shiPin) {
            holder.setBackgroundResource(R.id.bg_box, shiPin.getBg());
            holder.setText(R.id.title_shipin, shiPin.getTitle());
            holder.setText(R.id.context, shiPin.getContext());
        }
    }
}